import org.junit.Test;
import redis.clients.jedis.*;

import java.util.*;

public class JedisTest01 {
    /*连接到redis的java客户端*/
    @Test
    public void test01(){
        Jedis jedis = new Jedis("10.42.77.153",9000);
        Jedis jedis1 = new Jedis("10.42.77.153",6379);
        Jedis jedis2 = new Jedis("10.42.77.153",6380);
        Jedis jedis3 = new Jedis("10.42.77.153",6381);
        //基本命令
        jedis.del("name");
        jedis.exists("name");
        //String类型
        jedis.set("name","王老师");
        jedis.get("name");
        jedis.setex("bomb",500,"txt");
        //列表
        jedis.lpush("list01","100");
        jedis.lrange("list01",0,1);

    }
    @Test
    public void test02(){
        String key1 = new String("name-admin");
        String key2 = new String("name-admin");
        System.out.println(key1.equals(key2));
        System.out.println(key1.hashCode()&Integer.MAX_VALUE%3);
        System.out.println(key2.hashCode()&Integer.MAX_VALUE%3);
    }
    @Test
    public void test03(){
        List<Jedis> nodes = new ArrayList<>();
        nodes.add(new Jedis("10.42.77.153",6379));
        nodes.add(new Jedis("10.42.77.153",6380));
        nodes.add(new Jedis("10.42.77.153",6381));
        int j = new Random().nextInt(100);
        String keys="";
        for (int i = 0; i < 100; i++) {
            String key = UUID.randomUUID().toString();
            String value = "value_"+i;
            int index = getShard(key,nodes.size());
            nodes.get(index).set(key,value);
            if(i==j) {
                keys=key;
            }
        }
        int index = getShard(keys,nodes.size());
        System.out.println(nodes.get(index).get(keys));
    }
    //hash取余算法
    public int getShard(String key,int size){
        return (key.hashCode()&Integer.MAX_VALUE)%size;
    }
    //一致性哈希的分片对象sharderJedis
    @Test
    public void test04(){
        List<JedisShardInfo> nodes = new ArrayList<>();
        nodes.add(new JedisShardInfo(("10.42.77.153"),6379,500,500,3));
        nodes.add(new JedisShardInfo(("10.42.77.153"),6380));
        nodes.add(new JedisShardInfo(("10.42.77.153"),6381));
        //创建分片对象 api和jedis相同 但做了封装
        ShardedJedis sJedis = new ShardedJedis(nodes);
        /*sJedis.set("name","王老师");
        System.out.println(sJedis.get("name"));*/
        for (int i = 0; i < 1000; i++) {
            String key = UUID.randomUUID().toString();
            String value = "haha";
            sJedis.set(key,value);
        }
    }
    //连接池 从连接池获取资源
    @Test
    public void test05(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(200);
        //最大空闲 最小空闲
        config.setMaxIdle(10);
        config.setMinIdle(2);
        JedisPool pool = new JedisPool("10.42.77.153",6379);
        Jedis jedis = pool.getResource();
        //操作api
        jedis.close();
        //分片连接
        //手机界定啊信息
        List<JedisShardInfo> nodes = new ArrayList<>();
        nodes.add(new JedisShardInfo(("10.42.77.153"),6379,500,500,3));
        nodes.add(new JedisShardInfo(("10.42.77.153"),6380));
        nodes.add(new JedisShardInfo(("10.42.77.153"),6381));
        ShardedJedisPool pool1 = new ShardedJedisPool(config,nodes);
        ShardedJedis sJedis= pool1.getResource();
    }
    @Test
    public void test06(){
        //收集哨兵节点信息 保证哨兵节点在连接时不全部宕机
        Set<String> nodes = new HashSet<>();
        nodes.add("10.42.77.153:26379");
        JedisSentinelPool pool = new JedisSentinelPool("mymaster",nodes);
        //pool可以获取当前master信息
        HostAndPort master = pool.getCurrentHostMaster();
        System.out.println("当前主节点"+master.toString());
        Jedis jedis = pool.getResource();
        jedis.set("name","王老师");
        System.out.println(jedis.get("name"));
    }
    @Test
    public void test07(){
        //收集节点信息 只要保证这些节点在穿Gina初始化时候不同时宕机
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("10.42.77.153",8000));
        //参数
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(8);
        JedisCluster cluster = new JedisCluster(nodes,config);
        cluster.set("name","王老师");
        System.out.println(cluster.get("name"));
        ;

    }
}
